package com.library.weidget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.jtjrenren.android.taxi.passenger.R;

/**
 * Created by Administrator on 2016/3/2.
 */
public class RoundHView extends View {
    private int process = 0;
    private Paint yh_paint;
    private Paint jd_paint;

    public RoundHView(Context context) {
        super(context);
        initView();
    }



    public RoundHView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public RoundHView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        yh_paint = new Paint();
        yh_paint.setColor(getResources().getColor(R.color.radar_bg)); //设置圆环的颜色
        yh_paint.setStyle(Paint.Style.STROKE); //设置空心
        yh_paint.setStrokeWidth(4); //设置圆环的宽度
        yh_paint.setAntiAlias(true);  //消除锯齿

        jd_paint = new Paint();
        jd_paint.setColor(getResources().getColor(R.color.label_green));
        jd_paint.setStyle(Paint.Style.STROKE); //设置空心
        jd_paint.setStrokeWidth(4); //设置圆环的宽度
        jd_paint.setAntiAlias(true);  //消除锯齿
    }

    public void refresh(int process) {
        this.process = process;
        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int center_x = getWidth() / 2;
        int center_y = getHeight() / 2;

        int radius = getWidth() / 2 > getHeight() / 2 ? getHeight() / 2 : getWidth() / 2;

        //绘制外圆环
        int out_radius = radius - 5; //圆环的半径
        canvas.drawCircle(center_x, center_y, out_radius, yh_paint); //画出圆环

        //绘制进度
        RectF oval = new RectF(center_x - out_radius, center_y - out_radius, center_x
                + out_radius, center_y + out_radius);  //用于定义的圆弧的形状和大小的界限
        canvas.drawArc(oval, 270, 360 * process / 100, false, jd_paint);  //根据进度画圆弧
    }
}
